package yyn

import (
	"errors"
	"fmt"
	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/data/binding"
	"fyne.io/fyne/v2/data/validation"
	"fyne.io/fyne/v2/dialog"
	"fyne.io/fyne/v2/widget"
	"strconv"
)

var math = "^[+-]?\\d+(\\.\\d+)?([eE][+-]?\\d+)?$"
var mathNum = "^(k(\\d)*\\+\\d{3}){0,1}\\s*$"

//行对象
type RowDto struct {
	LonB binding.String //经度
	LatB binding.String //纬度
	NumB binding.String //桩号
}

func (a *RowDto) clear() {
	a.LonB.Set("")
	a.LatB.Set("")
	a.NumB.Set("")
}

//开始点
var sSite = RowDto{
	LonB: binding.NewString(), //经度
	LatB: binding.NewString(), //纬度
	NumB: binding.NewString(), //桩号
}

//结束点
var eSite = RowDto{
	LonB: binding.NewString(), //经度
	LatB: binding.NewString(), //纬度
	NumB: binding.NewString(), //桩号
}

//数据绑定容器
var DataSlice = make(map[int]RowDto, 3)

//经纬度转桩号
func CoordinateUI1(win fyne.Window) fyne.CanvasObject {
	title := widget.NewLabel("经纬度桩号转换")
	title.Alignment = fyne.TextAlignCenter

	//提示行
	tipLab1 := widget.NewLabel("经度")
	tipLab2 := widget.NewLabel("纬度")
	tipLab3 := widget.NewLabel("桩号")
	tipLab1.Alignment = fyne.TextAlignCenter
	tipLab2.Alignment = fyne.TextAlignCenter
	tipLab3.Alignment = fyne.TextAlignCenter
	tipItem := widget.NewFormItem("", container.NewGridWithColumns(3, tipLab1, tipLab2, tipLab3))

	//开始点
	slonEn := widget.NewEntryWithData(sSite.LonB)
	slatEn := widget.NewEntryWithData(sSite.LatB)
	sNumEn := widget.NewEntryWithData(sSite.NumB)
	sNumEn.Validator = validation.NewRegexp(mathNum, "格式 K123+123")
	slonEn.SetPlaceHolder("经度")
	slatEn.SetPlaceHolder("纬度")
	sNumEn.SetPlaceHolder("格式 K123+123")
	sItem := widget.NewFormItem("开始点", container.NewGridWithColumns(3, slonEn, slatEn, sNumEn))
	//结束点
	elonEn := widget.NewEntryWithData(eSite.LonB)
	elatEn := widget.NewEntryWithData(eSite.LatB)
	eNumEn := widget.NewEntryWithData(eSite.NumB)
	eNumEn.Validator = validation.NewRegexp(mathNum, "格式 K123+123")
	elonEn.SetPlaceHolder("经度")
	elatEn.SetPlaceHolder("纬度")
	eNumEn.SetPlaceHolder("格式 K123+123")
	eItem := widget.NewFormItem("结束点", container.NewGridWithColumns(3, elonEn, elatEn, eNumEn))
	eItem.HintText = "经纬度:保证小数点5位或更多, 桩号格式：k10590"
	form := &widget.Form{
		Items: []*widget.FormItem{
			tipItem,
			sItem,
			eItem,
		},
		SubmitText: "转换",
		CancelText: "清空",
		OnCancel: func() {
			fmt.Println("清空")
			initSiteData()

		},
		OnSubmit: func() {
			fmt.Println("计算")
			siteCalculate(win)
		},
	}

	//行
	row1 := createRow()
	row2 := createRow()
	row3 := createRow()
	row4 := createRow()
	row5 := createRow()
	contentForm := &widget.Form{
		Items: []*widget.FormItem{
			row1,
			row2,
			row3,
			row4,
			row5,
		},
	}
	initSiteData()
	return container.NewBorder(
		container.NewVBox(title, widget.NewSeparator(), form),
		nil,
		nil,
		nil,
		contentForm)
}

func createRow() *widget.FormItem {
	lonB := binding.NewString()
	latB := binding.NewString()
	numB := binding.NewString()
	dto := RowDto{
		LonB: lonB, //经度
		LatB: latB, //纬度
		NumB: numB, //桩号
	}
	//加入容器
	lenth := len(DataSlice)
	DataSlice[lenth+1] = dto

	lonEn := widget.NewEntryWithData(lonB)
	latEn := widget.NewEntryWithData(latB)
	numEn := widget.NewEntryWithData(numB)
	//lonEn.Validator = validation.NewRegexp(math, "请输入小数")
	//latEn.Validator = validation.NewRegexp(math, "请输入小数")
	numEn.Validator = validation.NewRegexp(mathNum, "格式 K123+123")
	lonEn.SetPlaceHolder("经度")
	latEn.SetPlaceHolder("纬度")
	numEn.SetPlaceHolder("格式 K123+123")

	return widget.NewFormItem(strconv.Itoa(lenth+1)+"", container.NewGridWithColumns(3, lonEn, latEn, numEn))
}

func initSiteData() {
	sSite.clear()
	eSite.clear()
	for _, value := range DataSlice {
		value.clear()
	}
}

//计算
func siteCalculate(win fyne.Window) {
	sLat, _ := sSite.LonB.Get()
	sLon, _ := sSite.LatB.Get()
	sNum, _ := sSite.NumB.Get()

	eLat, _ := sSite.LonB.Get()
	eLon, _ := sSite.LatB.Get()
	eNum, _ := sSite.NumB.Get()

	if sLat == "" || sLon == "" || sNum == "" || eLat == "" || eLon == "" || eNum == "" {
		err := errors.New("边界点经纬度必填 桩号必填")
		dialog.ShowError(err, win)
		return
	}
}

//func getBindValueString(bind binding.String) {
//	sLat, err := bind.Get()
//	if err != nil {
//		fmt.Println("data error")
//	}
//
//}
